package com.example.demo.service.imply;

import com.example.demo.mapper.DataMapper;
import com.example.demo.pojo.entity.Component;
import com.example.demo.pojo.entity.Storage;
import com.example.demo.pojo.vo.DataVO;
import com.example.demo.service.IDataService;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class IDataServiceImply implements IDataService {
    private final DataMapper dataMapper;
    @Override
    public DataVO init() {
        DataVO dataVO=new DataVO();
        ArrayList<Component>components=dataMapper.getComponent();
        ArrayList<Storage>storages=dataMapper.getStorage();
        LocalDateTime now = LocalDateTime.now();
        //统计基础数目
        int componentCount=components.size();
        int storageCount=storages.size();
        Integer todayOut;
        Integer todayIn;
        Integer totalDayIn;
        Integer totalDayOut;
        int goodStorageNum=0;
        int badStorageNum=0;
        for (Storage storage : storages) {
            if (storage.getStatus() == 1) {
                goodStorageNum++;
            } else {
                badStorageNum++;
            }
        }
        Integer inStorageNum=dataMapper.selectInComponent();
        List<Map<Integer,Integer>> categoryNum=dataMapper.selectCategory();
        ArrayList<Component>temp=new ArrayList<Component>();
        for(int i=0; i<components.size(); i++) {
            temp.add(components.get(i));
            if(i>9){
                break;
            }
        }
        dataVO.setComponents(temp);
        dataVO.setStorages(storages);
        dataVO.setCategoryNum(categoryNum.size());
        dataVO.setInStorageNum(inStorageNum);
        dataVO.setGoodStorageNum(goodStorageNum);
        dataVO.setBadStorageNum(badStorageNum);
        dataVO.setComponentCount(componentCount);
        dataVO.setStorageCount(storageCount);
        //日期逻辑处理
        ArrayList<Integer>inbounds = new ArrayList<Integer>();
        ArrayList<Integer>outbounds = new ArrayList<Integer>();
        ArrayList<LocalDateTime>timeTable=new ArrayList<>();
        ArrayList<Integer>componentDateCount=new ArrayList<>();
        ArrayList<Integer>storageDateCount=new ArrayList<>();
        todayIn=dataMapper.selectInboundByDate(now.minusDays(1),now);
        todayOut=dataMapper.selectOutboundByDate(now.minusDays(1),now);
        totalDayIn=dataMapper.selectInboundByDate(now.minusDays(1000),now);
        totalDayOut=dataMapper.selectOutboundByDate(now.minusDays(1000),now);
        for(int i=1;i<=7;i++){
            Integer dayIn=dataMapper.selectInboundByDate(now.minusDays(i),now.minusDays(i-1));
            Integer dayOut=dataMapper.selectOutboundByDate(now.minusDays(i),now.minusDays(i-1));
            Integer component=dataMapper.selectComponentByDate(now.minusDays(i),now.minusDays(i-1));
            Integer storage=dataMapper.selectStorageByDate(now.minusDays(i),now.minusDays(i-1));
            componentDateCount.add(0,component);
            storageDateCount.add(0,storage);
            timeTable.add(0,now.minusDays(i-1));
            inbounds.add(0,dayIn);
            outbounds.add(0,dayOut);
        }
        dataVO.setComponentCountByDate(componentDateCount);
        dataVO.setStorageCountByDate(storageDateCount);
        dataVO.setInboundCount(totalDayIn);
        dataVO.setOutboundCount(totalDayOut);
        dataVO.setInbounds(inbounds);
        dataVO.setOutbounds(outbounds);
        dataVO.setTodayIn(todayIn);
        dataVO.setTodayOut(todayOut);
        dataVO.setTimeTable(timeTable);
        return dataVO;
    }
}
